package ThirdCommunication;

import javax.swing.*;
import java.awt.*;

//私聊窗体类
public class PersonalChatFrame implements BasicData{
    private String myID;//当前用户ID
    private String toID;//聊天对象ID
    private JFrame jf;//窗体对象
    private JTextArea chat_One_showArea = new JTextArea(chat_showArea_row,chat_showArea_col);//消息显示区
    private JTextArea chat_One_inputArea = new JTextArea(chat_inputArea_row,chat_inputArea_col);//消息输入框
    private ClientThread clientthread;
    private Listener listener;
    private JFrame fileFrame;
    public PersonalChatFrame(String myID,String toID,ClientThread clientthread){
        this.myID = myID;
        this.toID = toID;
        this.clientthread = clientthread;
    }
    public void showUI(){
        jf = new JFrame(toID);
        jf.setSize(chat_Frame_width-250,chat_Frame_height);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setLayout(new FlowLayout());

        listener = new Listener(clientthread);
        listener.setMyID(myID);
        listener.setChat_One_inputArea(chat_One_inputArea);
        listener.setChat_One_showArea(chat_One_showArea);
        listener.setPersonalFrame(this);
        listener.setToID(toID);
        jf.add(chat_One_showArea);
        JLabel jlb = new JLabel("Input:");
        jf.add(jlb);
        jf.add(chat_One_inputArea);
        JButton jbu1 = new JButton("sendONE");//与群聊窗体的按钮命名区分开
        jbu1.addActionListener(listener);
        JButton jbu2 = new JButton("CLOSE");//关闭私聊窗口
        jbu2.addActionListener(listener);
        JButton jbu3 = new JButton("fileONE");//私发文件
        jbu3.addActionListener(listener);
        jf.add(jbu1);
        jf.add(jbu2);
        jf.add(jbu3);

        jf.setVisible(true);
        chat_One_inputArea.requestFocus();//设置鼠标光标在输入区域，设置光标要在设置窗体可见后
    }
    //消息显示
    public void showMessage(String message){
        chat_One_showArea.append(message+"\r\n");
    }
    //以设置窗体不可见来等效成关闭程序，若是退出程序会连带群聊窗体也一起退出
    public void close(){
        jf.setVisible(false);
    }
    public void showFileUI(){
        fileFrame = new JFrame("File");
        fileFrame.setSize(450, 300);
        fileFrame.setLocationRelativeTo(null);
        fileFrame.setDefaultCloseOperation(3);
        fileFrame.setLayout(new FlowLayout());

        JLabel jlb = new JLabel("File");
        fileFrame.add(jlb);
        //文件选择的文本框
        JTextField jtf = new JTextField();
        jtf.setPreferredSize(new Dimension(330,30));
        fileFrame.add(jtf);

        JButton jbu1 = new JButton("chooseFileONE");
        JButton jbu2 = new JButton("sendFileONE");
        jbu1.addActionListener(listener);
        jbu2.addActionListener(listener);
        fileFrame.add(jbu1);
        fileFrame.add(jbu2);
        listener.setFile_One_jtf(jtf);
        fileFrame.setVisible(true);
    }
    public void closeFileUI(){
        fileFrame.dispose();
    }
}

